import { defineStore } from 'pinia'
import request from '@/utils/request'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    userInfo: null
  }),

  actions: {
    // 发送验证码
    async sendSms(phone: string) {
      try {
        // const res = await request.post('/auth/sms/send', { phone })
        // return res.data
        return 123456
      } catch (error) {
        return Promise.reject(error)
      }
    },

    // 手机号登录
    async phoneLogin(data: { phone: string; sms: string }) {
      try {
        const res = await request.post('/auth/login/phone', data)
        this.token = res.data.token
        localStorage.setItem('token', res.data.token)
        return res.data
      } catch (error) {
        return Promise.reject(error)
      }
    },

    // 手机号注册
    async phoneRegister(data: { phone: string; sms: string; password: string }) {
      try {
        const res = await request.post('/auth/register/phone', data)
        return res.data
      } catch (error) {
        return Promise.reject(error)
      }
    },

    async login(data: { username: string; password: string }) {
      try {
        const res = await request.post('/user/login', data)
        this.token = res.token
        localStorage.setItem('token', res.token)
        return res
      } catch (error) {
        return Promise.reject(error)
      }
    },

    async getUserInfo() {
      try {
        const res = await request.get('/user/info')
        this.userInfo = res
        this.roles = res.roles
        return res
      } catch (error) {
        return Promise.reject(error)
      }
    },

    resetToken() {
      this.token = ''
      this.roles = []
      localStorage.removeItem('token')
    },

    async resetPassword(data: { account: string; sms: string; password: string }) {
      try {
        const res = await request.post('/auth/reset-password', data)
        return res
      } catch (error) {
        return Promise.reject(error)
      }
    },

    async smsLogin(data: { phone: string; sms: string }) {
      try {
        const res = await request.post('/auth/login/sms', data)
        this.token = res.token
        localStorage.setItem('token', res.token)
        return res
      } catch (error) {
        return Promise.reject(error)
      }
    },

    async register(data: { username: string; password: string }) {
      try {
        const res = await request.post('/auth/register', data)
        return res
      } catch (error) {
        return Promise.reject(error)
      }
    },

    async sendEmailCode(email: string) {
      try {
        const res = await request.post('/auth/email/code', { email })
        return res
      } catch (error) {
        return Promise.reject(error)
      }
    },

    async emailRegister(data: { email: string; code: string; password: string }) {
      try {
        const res = await request.post('/auth/register/email', data)
        return res
      } catch (error) {
        return Promise.reject(error)
      }
    }
  }
})
